<template>
  <view class="exportOutOrderList">
    <u-swipe-action v-if="fileList.length > 0">
      <u-swipe-action-item @click="swipeClick" v-for="(item, idx) in fileList" :options="item.options" :index="idx" :key="item.fileName" :title="item.fileName">
        <u-cell :title="item.fileName" @click="cellClick(item)">
          <u-icon slot="icon" color="#409eff" size="32" name="file-text-fill" />
          <u-icon slot="right-icon" color="#409eff" size="32" name="download" />
        </u-cell>
      </u-swipe-action-item>
    </u-swipe-action>
    <view v-else class="flca nodata">暂无数据</view>
    <u-modal :show="show" @confirm="confirm" @cancel="cancel" show-cancel-button title="确定要删除吗?" ref="uModal" />
  </view>
</template>
<script>
export default {
  data() {
    return {
      fileList: [],
      show: false,
      index: -1
    };
  },
  onLoad(option) {
    this.fileList = uni.getStorageSync('outOrder') || [];
    this.fileList.map(item => {
      item.options = [{ text: '删除', style: { backgroundColor: '#ff0000' } }];
    });
  },
  methods: {
    swipeClick(item, idx) {
      this.index = item.index;
      this.show = true;
    },
    cellClick(item) {
      uni.openDocument({
        filePath: `/storage/emulated/0/${item.filePath}${item.name}/${item.date}/${item.fileName}`, // 要加上前面/storage/emulated/0，不然打不开
        success: () => {},
        fail: err => {
          uni.showToast({ title: '打开失败', icon: 'error', duration: 2000 });
        }
      });
    },
    confirm() {
      this.show = false;
      let file = JSON.parse(JSON.stringify(this.fileList[this.index]));
      this.fileList.splice(this.index, 1);
      let list = JSON.parse(JSON.stringify(this.fileList));
      list.map(item => {
        delete item.options;
      });
      uni.setStorageSync('outOrder', list);
      uni.showToast({ title: `删除成功`, icon: 'success', duration: 2000 });
      // uni.removeSavedFile({
      //   filePath: `/storage/emulated/0/${file.filePath}${file.name}/${file.date}/${file.fileName}`,
      //   complete: res => {
      //     console.log(444, res);
      //   }
      // });
    },
    cancel() {
      this.show = false;
    }
  }
};
</script>
<style lang="scss">
.u-page {
  padding: 0;
}

.u-demo-block__title {
  padding: 10px 0 2px 15px;
}

.swipe-action {
  &__content {
    padding: 25rpx 0;

    &__text {
      font-size: 15px;
      color: $u-main-color;
      padding-left: 30rpx;
    }
  }
}
</style>
